//
//  PaintingViewModel.swift
//  NDAi
//
//  Created by 丸子 on 2024/6/27.
//

import UIKit

class PaintingViewModel {
    var isFirst:Bool = false
    var listModel:PaintingModel?
    var message:String?
    var negativePrompt:String = ""
    var style_id:Int?
    var proportion_id:Int?
    var imageList:[PaintImageDataModel] = []
    var page = 1
    let reloadDataSubject = PublishSubject<Void>()
    let createSuccessSubject = PublishSubject<String>()
    
    func getImageDataList(){
        NDProgressHUD.showLoading()
        RequestService.instance.imageDataList { model in
            NDProgressHUD.hide()
            self.listModel = model
            if let styleModel = model.style?.first {
                self.style_id = styleModel.id ?? 0
                if let model = Appearance.shared.recordStyle {
                    self.style_id = model.style_id
                }
            }
            if let pModel = model.proportion?.first {
                self.proportion_id = pModel.id ?? 0
                if let model = Appearance.shared.recordStyle {
                    self.proportion_id = model.proportion_id
                }
            }
            if let model = Appearance.shared.recordStyle {
                self.message = model.message ?? ""
            }
            self.reloadDataSubject.onNext(())
        } failureClosure: { error in
            if !self.isFirst {
                self.isFirst = true
                DispatchQueue.global().asyncAfter(deadline: .now() + 5) { [weak self] in
                    self?.getImageDataList()
                }
            }
            NDProgressHUD.hide()
        }
    }
    
    func imageCreate(isOpen:Bool){
        guard let styleId = self.style_id, let proportion_id = self.proportion_id else { return }
        if self.message == nil {
            NDProgressHUD.showError(title: "", subtitle: "请输入描述词")
            return
        }
        if let message = self.message, message.count < 10 {
            NDProgressHUD.showError(title: "", subtitle: "描述词不能少于10个字，请完善描述词")
            return
        }
        if !isOpen {
            self.negativePrompt = ""
        }
        NDProgressHUD.showLoading()
        RequestService.instance.imageCreate(message: self.message ?? "", negativePrompt: self.negativePrompt, style_id: styleId, proportion_id: proportion_id) { id in
            NDProgressHUD.hide()
            self.createSuccessSubject.onNext(id)
        } failureClosure: { error in
            NDProgressHUD.hide()
        }
        
    }
     
    
    
    
    func getHomeDrawList(){
        RequestService.instance.homeDrawList(page: self.page) {[weak self] list in
            guard let self = self else { return }
            let dataArr = list ?? []
            if self.page == 1 {
                self.imageList = []
            }
            if self.imageList.count > 0 {
                self.imageList += dataArr
            }else {
                self.imageList = dataArr
            }
            self.reloadDataSubject.onNext(())
        } failureClosure: { error in
            
        }
    }
    
}
